// DriveNotes. Copyright (c) 2011, Andrei Senchuk. All rights reserved.
package net.taviscaron.drivenotes.server.model.validators;

import net.taviscaron.drivenotes.server.model.dao.ChargingDAO;
import net.taviscaron.drivenotes.shared.exceptions.InvalidEntityException;
import net.taviscaron.drivenotes.shared.model.entity.Charging;
import org.springframework.beans.factory.annotation.Required;
import static net.taviscaron.drivenotes.server.util.ExceptionUtils.invalidEntityException;

/**
 * Charging validator
 * @author Andrei Senchuk
 */
public class ChargingValidator implements Validator<Charging> {
    private static final int FILLING_STATION_MAX_SIZE = 2048;

    public enum Error {
        INVALID_ODOMETR_VALUE, INVALID_PRICE,
        INVALID_VOLUME, FILLING_STATION_IS_TOO_LONG
    }

    private ChargingDAO chargingDAO;

    @Required
    public void setChargingDAO(ChargingDAO chargingDAO) {
        this.chargingDAO = chargingDAO;
    }

    public void validate(Charging charging) throws InvalidEntityException {
        if(charging.getOdometrValue() == null || charging.getOdometrValue() < 0) {
            throw invalidEntityException(Error.INVALID_ODOMETR_VALUE);
        }

        if(charging.getPrice() == null || charging.getPrice() <= 0) {
            throw invalidEntityException(Error.INVALID_PRICE);
        }

        if(charging.getVolume() == null || charging.getVolume() <= 0) {
            throw invalidEntityException(Error.INVALID_VOLUME);
        }

        if(charging.getFillingStation() != null && charging.getFillingStation().length() > FILLING_STATION_MAX_SIZE) {
            throw invalidEntityException(Error.FILLING_STATION_IS_TOO_LONG);
        }
    }
}
